ID_PartsParents <- function(PartTree) {
  
  # This function is designed to identify the parents of each component, 
  # and prepare them for plotting
  #
  # Inputs
  #   PartTree - 1xN vector - gives the part's level in the hierarch, higher = down rabbit hole
  #     Since this is for a specific rabbit hole, the highest level can have more than one entry.
  #
  # Outputs
  #   ParentIdx - 1xN list of vectors - gives the list of parents for each component
  #
  # Written by: Sean M Gonzalez, Willcor Inc., October 2012
  #_____________________________________________________________________
  
  # Now that we have the component sub-branches, we need to list components' parents
  N = length(PartTree)
  ParentIdx <- list()
  ParentIdx[[1]] = 0 # skip the top component
  listCount = 1
  
  for (c in 2:N) {
    # Identify parent indexes
    componentLevel = PartTree[c]
    numParents = componentLevel -1
    parentLevelFound = componentLevel
    px = 0
    parents <- integer(numParents)
    
    for (p in c:1) {
      if (PartTree[p] < parentLevelFound) {
        px = px +1
        parents[px] = p
        parentLevelFound = PartTree[p]
#         parents = c(parents,p); #[px] = p; #PartTree[p]
      }
    }
    # Check results
    if (px != numParents) {print(c("Component Level wasn't a predictor for part #",p))}
    if (any(parents==0)) {print(c("Parent =0 found for part #",p))}
    
    # Keep track and count
    listCount = listCount +px
    ParentIdx[[c]] <- parents
  }
  Output <- list("ParentIdx"=ParentIdx)
  
  # Need to count each component's parent list
  parentSum=0
  for (c in 1:length(ParentIdx)) {
    parents = ParentIdx[[c]]
    parentSum = parentSum +length(parents)
  }
  if (parentSum != listCount) {print("Lists aren't adding up!")}
  
  # Create the associative list
  parentAssocList <- integer(2*N)
  pal = 0
  for (c in 1:N) {
    parents = ParentIdx[[c]]
    parentAssocList[2*pal+(1:2)] = c(c,parents[1])
    pal = pal +1
  }
  Output$ParentAssocList <- parentAssocList
  
#   library(igraph)
#   parentAssocGraph <- graph(parentAssocList, n=max(parentAssocList), directed=FALSE);
#   plot.igraph(parentAssocGraph,layout=layout.fruchterman.reingold,vertex.size=12) #,layout=layout.kamada.kawai)
  
  return(Output)
}